package jjn.daily;

import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/24 23:48
 */
public class LeetCode714 {
    public int maxProfit(int[] prices, int fee) {
        int[][] profit = new int[prices.length][2];
        profit[0][0] = 0;
        profit[0][1] = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            // 已经持有股票,卖出去
            profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i] - fee);
            profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - prices[i]);
        }
        return profit[prices.length - 1][0];
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int fee = scanner.nextInt();
        int total = scanner.nextInt();
        int[] prices = new int[total];
        for (int i = 0; i < total; i++) {
            prices[i] = scanner.nextInt();
        }
        int maxProfit = new LeetCode714().maxProfit(prices, fee);
        System.out.println(maxProfit);
    }
}
